package com.d404.server;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

import com.alibaba.fastjson.JSON;

import im.beans.Classify;
import im.beans.User;
import manager.PersistentManager;

/**
 * Servlet implementation class MoveFriend
 */
//@WebServlet("/MoveFriend")
public class MoveFriend extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public MoveFriend() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//		response.getWriter().append("Served at: ").append(request.getContextPath());
		
		request.setCharacterEncoding("UTF-8");
		String userName = request.getParameter("userName");
		String userID = request.getParameter("userID");
		String friendName = request.getParameter("friendName");
		String friendID = request.getParameter("friendID");
		
		String _class = request.getParameter("_class");
		String _classID = request.getParameter("_classID");
		
		String _beforeClassID = request.getParameter("_beforeClassID");
		
		String status = "9";
		
		Download download = new Download();
		
		SessionFactory sf = PersistentManager.sharemanager().getSessionFactory();
//		Session session = sf.openSession();
		
		
		
		Session session = sf.getCurrentSession();
		Transaction tx = session.beginTransaction();
		try
		{
			Query q = session.createQuery("update Classify_User cu set cu.classify = ? where cu.classify = ? and cu.user = ?");
			Classify cs = new Classify();
			cs.setId(Long.parseLong(_classID));
			q.setParameter(0, cs);
			Classify beforeCs = new Classify();
			beforeCs.setId(Long.parseLong(_beforeClassID));
			q.setParameter(1, beforeCs);
			
			User u = new User();
//			u = (User) session.load(User.class, Long.parseLong(friendID));
			u.setId(Long.parseLong(friendID));
			q.setParameter(2, u);
			q.executeUpdate();
			session.getTransaction().commit();
			download.status = Download.SUCCESS;
		}catch(Exception e)
		{
			tx.rollback();
			download.status = Download.FAILED;
			e.printStackTrace();
		}finally {
//			session.close();
		}
		
		response.setCharacterEncoding("UTF-8");
		System.out.println(JSON.toJSONString(download));
		response.getWriter().append(JSON.toJSONString(download));
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	 // 下行数据
    public static class Download
    {
        public static final int SUCCESS = 1;
        public static final int FAILED = 2;

        int status;

        public int getStatus() {
            return status;
        }

        public void setStatus(int status) {
            this.status = status;
        }
    }

}
